package Server.ExtComponents.BaseConfig;

import org.aeonbits.owner.ConfigFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class MServerConfig {
    private static final Logger LOG = LoggerFactory.getLogger(MServerConfig.class);
    public static  boolean debug=true;//true:调试打印日志；false:运行打印日志
    public static  boolean isRunning=true;//server hand data Thread
    public  static  boolean NETTY_DEBUG=false;
    public static  int readTimeOut=0;//read data time out   ..secs
    public static  int writeTimeOut=0;//write data time out    ..secs
    public static  int GameserverPort=0;//server port
    public static  boolean isSafeopen=false;//是否打开安全沙箱
    public static  int maxConnection=0;//最大连接数

    public static  int ExchangeChips=0;//
     //数据包解析参数
    public static  int maxFrameLength=0;
    public static  int lengthFieldOffset   = 0 ;
    public static  int lengthFieldLength   = 0 ;
    public static  int lengthAdjustment    = 0 ;
    public static  int initialBytesToStrip = 0;



    //业务处理线程池配置（netty自带框架）ExecutionHandler， coresize=wodrker线程数的1/2
    public static final int ExecutionHandler=Runtime.getRuntime().availableProcessors()+1;
    public static  long maxChannelMemorySize=0;
    public static  long maxTotalMemorySize=0;
    public static  int   Idle=0;//MemoryAwareThreadPoolExecutor thread idle
    public static long checkInterval=0;//监测流量间隔

    public static  int   AIThreadNums=0;//ai线程池配置
    public static  int   BattleThreadNums=0;//battle线程池配置
    public static  int WriteBufNum=0;//poll all session buf

    public static  int TableNum=0;//TableNum
    public static long CountPacktime=0;//#记录接收包周期默认2秒
    public static String SigarPath="";

    /**
     * 初始化 netty 配置
     * @return
     */
    public  boolean IntiServerConfig(){
        boolean Status = false;
        try {
           BaseServerConfig baseConfig= ConfigFactory.create(BaseServerConfig.class);
            debug = baseConfig.debug();
            isRunning=baseConfig.isRunning();
            GameserverPort = baseConfig.GameserverPort();
            NETTY_DEBUG=baseConfig.NETTY_DEBUG();
            readTimeOut = baseConfig.readTimeOut();

            writeTimeOut = baseConfig.writeTimeOut();
            isSafeopen =baseConfig.isSafeopen();
            maxConnection=baseConfig.maxConnection();

            ExchangeChips=baseConfig.ExchangeChips();
            //数据包协议解析
            maxFrameLength =baseConfig.maxFrameLength();
            lengthFieldOffset =baseConfig.lengthFieldOffset();
            lengthFieldLength =baseConfig.lengthFieldLength();
            lengthAdjustment =baseConfig.lengthAdjustment();
            initialBytesToStrip =baseConfig.initialBytesToStrip();

            //业务线程池配置（netty自带框架）
            maxChannelMemorySize=baseConfig.maxChannelMemorySize();
            maxTotalMemorySize=baseConfig.maxTotalMemorySize();
            Idle=baseConfig.Idle();
            AIThreadNums=baseConfig.AIThreadNums();
            BattleThreadNums=baseConfig.BattleThreadNums();
            WriteBufNum=baseConfig.WriteBufNum();
            TableNum=baseConfig.TableNum();
            CountPacktime=baseConfig.CountPacktime();
            checkInterval=baseConfig.checkInterval();

            //config singar path
            SigarPath=baseConfig.SigarPath();
                if (System.getProperty("os.name").startsWith("W")){
                    copySigarFile(config.Windows_SIGAR_PATH,System.getProperty("java.home")+"/bin/");
                    System.setProperty("org.hyperic.sigar.path", System.getProperty("java.home")+"/bin/");
                }else {
                    copySigarFile(config.Linux_SIGAR_PATH,SigarPath);
                    System.setProperty("org.hyperic.sigar.path", SigarPath);
                }

            Status = true;
        } catch (Exception ex) {
            LOG.error("MServerConfig 读取系统基础定义文件出错！", ex);
        }
        return Status;
    }

    private  void copySigarFile(String sourceFileUrl,String targetUrl) throws IOException {
        try {
            int bantered;
            File oldfield = new File(sourceFileUrl);
            String toPath = targetUrl+""+oldfield.getName().trim();
            if (oldfield.exists()) {
                InputStream inStream1 = new FileInputStream(sourceFileUrl);
                File toFile = new File(toPath);
                FileOutputStream fs1 = new FileOutputStream(toFile);
                if (!toFile.exists()){
                    toFile.mkdirs();
                }
                byte[] buffer = new byte[1024];
                while ( (bantered = inStream1.read(buffer)) != -1) {
                    fs1.write(buffer, 0, bantered);
                }
                inStream1.close();
                fs1.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}
